#include "messager.h"
#include "ams.h"
#include "log.h"
#include "lvgl.h"
#include "track_service.h"

LV_EVENT_CB_DECLARE(compress_slider_event_cb);

static void *(*slider_load_handle)(void *a, void *b) = NULL;
static void *(*slider_unload_handle)(void *a, void *b) = NULL;
static lv_obj_t *lv_screen = NULL;

void *track_create_slider(void *content, uint32_t value, void *(*load)(void *a, void *b), void *(*unload)(void *a, void *b));

/**
 * @description:
 * @param {void} *content
 * @param {void} *
 * @param {void} *b
 * @return {*}
 */
void *track_create_slider(void *content, uint32_t value, void *(*load)(void *a, void *b), void *(*unload)(void *a, void *b))
{
    lv_screen = (lv_obj_t *)content;
    lv_obj_t *slider = lv_slider_create(content, NULL);
    lv_obj_set_size(slider, 4, 320);
    lv_obj_align(slider, NULL, LV_ALIGN_IN_LEFT_MID, 10, 30);
    lv_obj_set_event_cb(slider, compress_slider_event_cb);
    lv_slider_set_range(slider, 0, 200);
    lv_slider_set_value(slider, value, LV_ANIM_OFF);
    slider_load_handle = load;
    slider_unload_handle = unload;

    return slider;
}

LV_EVENT_CB_DECLARE(compress_slider_event_cb)
{
    if (e == LV_EVENT_VALUE_CHANGED)
    {
        uint32_t value = lv_slider_get_value(obj);
        if (slider_load_handle && slider_unload_handle)
        {
            slider_unload_handle(lv_screen, &value);
            slider_load_handle(lv_screen, &value);
        }
    }
}
